package com.zxshhy.zookeeper;

import lombok.extern.slf4j.Slf4j;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Slf4j
@Configuration
public class ZKClientBase {

//    @Bean
    public ZkClient init(){
        ZkClient zkClient = new ZkClient("52.81.152.99:2181", 3000);
        log.info("连接zookeeper信息：{}",zkClient.getChildren("/"));
        return zkClient;
    }

    public static void main(String[] args){
        ZkClient zkClient = new ZkClient("52.81.152.99:2181", 3000);
//        ZkClient zkClient = new ZkClient(new ZKConnection("192.168.99.121:2181,192.168.99.122:2181,192.168.99.123:2181"),50000);
        //递归创建节点 缺点在于并没有为节点指定VALUE
        zkClient.createPersistent("/test/zkclient",true);
        zkClient.writeData("/test","test");
        System.out.println("/test value is " + zkClient.readData("/test"));
        zkClient.createPersistent("/test/zkclient!c1");
        zkClient.createPersistent("/test/zkclient/c2");
        List<String> children = zkClient.getChildren("/test/zkclient");
        for(String str : children){
            System.out.println("获取到zookeeper节点信息：" + str);
        }
        System.out.println("size :  "+ zkClient.countChildren("/test/zkclient"));
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//递归删除
        zkClient.deleteRecursive("/test");
    }
}
